package com.faceplusplus.apitest;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.faceplusplus.api.FaceDetecter;
import com.faceplusplus.api.FaceDetecter.Face;
import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;
import com.megvii.apitest.R;

public class MainActivity extends Activity {
	// key值
	public final static String APIKEY = "dbbc77fb6f9f5ba3322d4e9a6e9f1f81";
	public final static String APISECRET = "7OA_1ztOi6E2stodI-1p5K5sKYdVJlbt";
	private final static int REQUEST_GET_PHOTO = 1;

	private Bitmap curBitmap;
	ImageView imageView = null;
	HandlerThread detectThread = null;
	Handler detectHandler = null;
	FaceDetecter detecter = null;
	HttpRequests request = null;// 在线api

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		imageView = (ImageView) findViewById(R.id.imageview);
		curBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.demo);
		imageView.setImageBitmap(curBitmap);

		detectThread = new HandlerThread("detect");
		detectThread.start();
		detectHandler = new Handler(detectThread.getLooper());

		detecter = new FaceDetecter();
		detecter.init(this, APIKEY);// 初始化

		request = new HttpRequests(APIKEY, APISECRET);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		detecter.release(this);// 释放引擎
	}

	/**
	 * 读取识别头像范围
	 * 
	 * @param faceinfos
	 * @param oribitmap
	 * @return
	 */
	public static Bitmap getFaceInfoBitmap(Face[] faceinfos, Bitmap oribitmap) {
		Bitmap tmp;
		tmp = oribitmap.copy(Bitmap.Config.ARGB_8888, true);

		Canvas localCanvas = new Canvas(tmp);
		Paint localPaint = new Paint();
		localPaint.setColor(0xffff0000);
		localPaint.setStyle(Paint.Style.STROKE);

		for (Face localFaceInfo : faceinfos) {
			Log.i("diff", "trackingID=" + localFaceInfo.trackingID);
			RectF rect = new RectF(oribitmap.getWidth() * localFaceInfo.left,
					oribitmap.getHeight() * localFaceInfo.top,
					oribitmap.getWidth() * localFaceInfo.right,
					oribitmap.getHeight() * localFaceInfo.bottom);
			localCanvas.drawRect(rect, localPaint);
		}
		return tmp;
	}

	/**
	 * 读取图形文件
	 * 
	 * @param fileName
	 * @param dstWidth
	 * @return
	 */
	public static Bitmap getScaledBitmap(String fileName, int dstWidth) {
		BitmapFactory.Options localOptions = new BitmapFactory.Options();
		localOptions.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(fileName, localOptions);
		int originWidth = localOptions.outWidth;
		int originHeight = localOptions.outHeight;

		localOptions.inSampleSize = originWidth > originHeight ? originWidth
				/ dstWidth : originHeight / dstWidth;
		localOptions.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(fileName, localOptions);
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.camera:// 摄像头
			startActivity(new Intent(MainActivity.this, CameraPreview.class));
			break;
		case R.id.pick:// 相册
			startActivityForResult(new Intent("android.intent.action.PICK",
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
					REQUEST_GET_PHOTO);
			break;
		case R.id.detect:// 检测
			detectHandler.post(new Runnable() {

				@Override
				public void run() {

					Face[] faceinfo = detecter.findFaces(curBitmap);// 进行人脸检测
					if (faceinfo == null) {
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								Toast.makeText(MainActivity.this, "未发现人脸信息",
										Toast.LENGTH_LONG).show();
							}
						});
						return;
					}

					// 在线api交互
					try {
						String resultJson = detecter.getResultJsonString();
						if (resultJson != null) {
							Log.i("diff", "detecter resultJson=" + resultJson);
						}
						request.offlineDetect(detecter.getImageByteArray(),
								detecter.getResultJsonString(),
								new PostParameters());
					} catch (FaceppParseException e) {
						e.printStackTrace();
					}
					final Bitmap bit = getFaceInfoBitmap(faceinfo, curBitmap);

					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							imageView.setImageBitmap(bit);
							System.gc();
						}
					});
				}
			});
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case REQUEST_GET_PHOTO: {
				if (data != null) {
					final String str;
					Uri localUri = data.getData();
					String[] arrayOfString = new String[1];
					arrayOfString[0] = "_data";
					Cursor localCursor = getContentResolver().query(localUri,
							arrayOfString, null, null, null);
					if (localCursor == null)
						return;
					localCursor.moveToFirst();
					str = localCursor.getString(localCursor
							.getColumnIndex(arrayOfString[0]));
					localCursor.close();
					if ((curBitmap != null) && (!curBitmap.isRecycled()))
						curBitmap.recycle();
					curBitmap = getScaledBitmap(str, 600);
					imageView.setImageBitmap(curBitmap);
				}
				break;
			}
			}

		}
	}
}
